package com.seth.myapplication;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.seth.myapplication.bean.User;
import com.seth.myapplication.helper.SetStatusTabHeight;
import com.seth.myapplication.surface.page1Fragment;
import com.seth.myapplication.surface.page2Fragment;
import com.seth.myapplication.surface.page3Fragment;
import com.seth.myapplication.surface.page4Fragment;

/**
 * Created by Seth on 2017/9/26.
 */

public class MainActivity extends FragmentActivity{

    private static Fragment contentFragment;
    private Fragment firstPage;
    private Fragment secondPage;
    private Fragment thirdPage;
    private Fragment forthPage;

    private FragmentManager fm;

    private LinearLayout firstBlock;
    private LinearLayout secondBlock;
    private LinearLayout thirdBlock;
    private LinearLayout forthBlock;
    private LinearLayout topStatus;

    private TextView firstTabTxt;
    private TextView secondTabTxt;
    private TextView thirdTabTxt;
    private TextView forthTabTxt;

    private ImageView firstTabImg;
    private ImageView secondTabImg;
    private ImageView thirdTabImg;
    private ImageView forthTabImg;

    private Resources mResources;
    private ColorStateList cls1,cls2;

//    private User mUser;


    public static void setContentFragment(Fragment fragment) {
        contentFragment = fragment;
    }

    public static Fragment getContentFragment() {
        return contentFragment;
    }


    public Fragment getSecondPage() {
        return secondPage;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        setContentView(R.layout.activity_main);

        initView();
        initEvent();

//        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
//
//        if (fragment == null) {
//            fragment = new page1Fragment();
//            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
//        }
    }

    private void initEvent() {
//        mUser = new User();
        if(firstPage == null){
            firstPage = new page1Fragment();
            fm.beginTransaction().add(R.id.fragment_container, firstPage).commit();
            contentFragment = firstPage;

        }
        firstBlock.setBackgroundResource(R.drawable.bottom_tab_selected);
        firstTabTxt.setTextColor(cls1);
        firstTabImg.setImageResource(R.drawable.homepage_fill);



    }

    private void initView() {
        /*设置标题栏高度*/
        topStatus = (LinearLayout) findViewById(R.id.top_sts);
//        /*动态获取高度*/
//        int height = getStatusBarHeight(this);
//        /*设置高度*/
//        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) topStatus.getLayoutParams();
//        params.height = height;
//        topStatus.setLayoutParams(params);

        new SetStatusTabHeight(topStatus,this).setRelativeLayout();

        firstBlock = (LinearLayout) findViewById(R.id.tab_1);
        secondBlock = (LinearLayout) findViewById(R.id.tab_2);
        thirdBlock = (LinearLayout) findViewById(R.id.tab_3);
        forthBlock = (LinearLayout) findViewById(R.id.tab_4);

        firstTabTxt = (TextView) findViewById(R.id.tab_first_txt);
        secondTabTxt = (TextView) findViewById(R.id.tab_second_txt);
        thirdTabTxt = (TextView) findViewById(R.id.tab_third_txt);
        forthTabTxt = (TextView) findViewById(R.id.tab_forth_txt);

        firstTabImg = (ImageView) findViewById(R.id.tab_first_img);
        secondTabImg = (ImageView) findViewById(R.id.tab_second_img);
        thirdTabImg = (ImageView) findViewById(R.id.tab_third_img);
        forthTabImg = (ImageView) findViewById(R.id.tab_forth_img);

        fm = getSupportFragmentManager();
        firstPage = fm.findFragmentById(R.id.fragment_container);
        secondPage = fm.findFragmentById(R.id.fragment_container);
        thirdPage = fm.findFragmentById(R.id.fragment_container);
        forthPage = fm.findFragmentById(R.id.fragment_container);

        mResources = (Resources) getBaseContext().getResources();
        cls1 = (ColorStateList) mResources.getColorStateList(R.color.default_backgroud_color);
        cls2 = (ColorStateList) mResources.getColorStateList(R.color.tab_txt);

    }

    //fragment替换方法
    private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment){
        if(contentFragment == fragment){
            return ;
        }

        if(!fragment.isAdded()){    //如果当前fragment为被添加，则添加到Fragment管理器中
            transaction.hide(contentFragment).add(R.id.fragment_container, fragment).commit();
        }else{
            transaction.hide(contentFragment).show(fragment).commit();
        }
        contentFragment = fragment;
    }

    public void ToForthPage(View view){
        tabUnselected();
        if (forthPage == null){
            forthPage = new page4Fragment();
        }
        addOrShowFragment(fm.beginTransaction(), forthPage);
        forthBlock.setBackgroundResource(R.drawable.bottom_tab_selected);
        forthTabTxt.setTextColor(cls1);
        forthTabImg.setImageResource(R.drawable.emoji_fill);
        contentFragment = forthPage;

    }
    public void ToFirstPage(View view){
        tabUnselected();
        if (firstPage == null){
            firstPage = new page1Fragment();
        }
        addOrShowFragment(fm.beginTransaction(), firstPage);
        firstBlock.setBackgroundResource(R.drawable.bottom_tab_selected);
        firstTabTxt.setTextColor(cls1);
        firstTabImg.setImageResource(R.drawable.homepage_fill);
        contentFragment = firstPage;
        page1Fragment.setScrollView();
    }

    public void ToThirdPage(View view){
        tabUnselected();
        if(thirdPage == null){
            thirdPage = new page3Fragment();
        }
        addOrShowFragment(fm.beginTransaction(), thirdPage);
        thirdBlock.setBackgroundResource(R.drawable.bottom_tab_selected);
        thirdTabTxt.setTextColor(cls1);
        thirdTabImg.setImageResource(R.drawable.order_fill);
        contentFragment = thirdPage;
    }
    public void ToSecondPage(View view){
        tabUnselected();
        if(secondPage == null){
            secondPage = new page2Fragment();
//            Bundle bundle = new Bundle();
//            bundle.putSerializable(page2Fragment.PASS_USER,mUser);
//            secondPage.setArguments(bundle);
            ;
        }
        addOrShowFragment(fm.beginTransaction(), secondPage);
        secondBlock.setBackgroundResource(R.drawable.bottom_tab_selected);
        secondTabTxt.setTextColor(cls1);
        secondTabImg.setImageResource(R.drawable.addition_fill);
        contentFragment = secondPage;
    }


//    /**
//     * 获取状态栏高度
//     * @param context
//     * @return
//     */
//    private int getStatusBarHeight(Context context) {
//        int result = 0;
//        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
//        if (resourceId > 0) {
//            result = context.getResources().getDimensionPixelSize(resourceId);
//        }
//        return result;
//    }

    private void tabUnselected(){
        if(contentFragment==firstPage){
            firstBlock.setBackgroundResource(R.drawable.bottom_tab_unselected);
            firstTabTxt.setTextColor(cls2);
            firstTabImg.setImageResource(R.drawable.homepage);
        }else if(contentFragment==secondPage){
            secondBlock.setBackgroundResource(R.drawable.bottom_tab_unselected);
            secondTabTxt.setTextColor(cls2);
            secondTabImg.setImageResource(R.drawable.addition);
        }else if(contentFragment==thirdPage){
            thirdBlock.setBackgroundResource(R.drawable.bottom_tab_unselected);
            thirdTabTxt.setTextColor(cls2);
            thirdTabImg.setImageResource(R.drawable.order);
        }else if(contentFragment==forthPage){
            forthBlock.setBackgroundResource(R.drawable.bottom_tab_unselected);
            forthTabTxt.setTextColor(cls2);
            forthTabImg.setImageResource(R.drawable.emoji);
        }else{
            secondBlock.setBackgroundResource(R.drawable.bottom_tab_unselected);
            secondTabTxt.setTextColor(cls2);
            secondTabImg.setImageResource(R.drawable.addition);
        }
    }

    public void ResetTask() {
        secondPage = new page2Fragment();
    }
}
